#include "GameRender.h"
#include <Windows.h>
#include "Logic/Role.h"
#include "Visual.h"
#include "Message/MessagePump.h"
#include "Logic/Entity/EntityManager.h"

extern DXDraw* g_pDraw;
extern Role*   g_pRole;
extern Role*   g_pEnemy;


void EntityMoveCallback::function(void* parent, Message* msg)
{
	LogicMsg data = ((DataMessage<LogicMsg>*)msg)->m_dataStorage;
	RenderMessage* pRenderMsg = new RenderMessage(MSGTYPE_ENTITY_MOVE);
	pRenderMsg->m_cell1 = data.from;
	pRenderMsg->m_cell2 = data.to;
	pRenderMsg->m_data3 = data.direction;
	pRenderMsg->m_time  = data.time;
	int id = msg->m_toID;
	BaseGameEntity* pEntity = s_EntityManager->GetEntityFromID(id);
	pEntity->GetVisual()->AddRenderMessage(pRenderMsg);
}

void EntityStopCallback::function(void* parent, Message* msg)
{
	int id = msg->m_toID;
	BaseGameEntity* pEntity = s_EntityManager->GetEntityFromID(id);
	RenderMessage* pRenderMsg = new RenderMessage(MSGTYPE_ENTITY_STOP);
	pEntity->GetVisual()->AddRenderMessage(pRenderMsg);
}

void EntityHurtCallback::function(void* parent, Message* msg)
{
	int id = msg->m_toID;
	BaseGameEntity* pEntity = s_EntityManager->GetEntityFromID(id);
	RenderMessage* pRenderMsg = new RenderMessage(MSGTYPE_ENTITY_HURT);
	pEntity->GetVisual()->AddRenderMessage(pRenderMsg);
}

void EntityAttackCallback::function(void* parent, Message* msg)
{
	LogicMsg data = ((DataMessage<LogicMsg>*)msg)->m_dataStorage;
	RenderMessage* pRenderMsg = new RenderMessage(MSGTYPE_ENTITY_ATTACK);
	pRenderMsg->m_data3 = data.direction;
	pRenderMsg->m_time  = data.time;
	int id = msg->m_toID;
	BaseGameEntity* pEntity = s_EntityManager->GetEntityFromID(id);
	pEntity->GetVisual()->AddRenderMessage(pRenderMsg);
}



void GameRender::HandleEntityMove(const LogicMsg& data)
{
	RenderMessage* pRenderMsg = new RenderMessage(MSGTYPE_ENTITY_MOVE);
	pRenderMsg->m_cell1 = data.from;
	pRenderMsg->m_cell2 = data.to;
	pRenderMsg->m_data3 = data.direction;
	pRenderMsg->m_time  = data.time;
	g_pRole->GetVisual()->AddRenderMessage(pRenderMsg);
}

void GameRender::AddRenderMessage(RenderMessage* pMsg)
{
	m_renderMsgList.push_back(pMsg);
}
GameRender::GameRender(Animate* ani,Animate* ani2)
{
	m_lastTime = 0;
	s_MessagePump->RegisterForMessage(MODULE_MSG, MSGTYPE_ENTITY_MOVE,this,-1,m_entityMoveCallback);
	s_MessagePump->RegisterForMessage(MODULE_MSG, MSGTYPE_ENTITY_STOP,this,-1,m_entityStopCallback);
	s_MessagePump->RegisterForMessage(MODULE_MSG, MSGTYPE_ENTITY_HURT,this, -1,m_entityHurtCallback);
	s_MessagePump->RegisterForMessage(MODULE_MSG, MSGTYPE_ENTITY_ATTACK,this, -1,m_entityAttackCallback);

	// for testing.
	Animate* anit;
	RenderData data;

	for (int i = 0; i < 5; i++)
	{
		anit = new Animate("data/m_ef07", NULL, false);
		data.point.x = (i*3 + 6)*32 + 16;
		data.point.y = 8*32 + 16;
		data.time = 25 * 12;
		anit->SetRenderData(data);
		m_aniList.push_back(anit);
	}

	for (int i = 0; i < 15; i++)
	{
		anit = new Animate("data/fireball", NULL, false);
		data.point.x = (i + 5)*32 + 16;
		data.point.y = 13*32 + 16;
		anit->SetRenderData(data);
		m_aniList.push_back(anit);
	}

	for (int i = 0; i < 2; i++)
	{
		anit = new Animate("data/05vallentine", NULL, false);
		data.point.x = (i*4 + 5)*32 + 16;
		data.point.y = 5*32 + 16;
		data.time = 25*50;
		anit->SetRenderData(data);
		m_aniList.push_back(anit);
	}

	for (int i = 3; i < 5; i++)
	{
		anit = new Animate("data/fast", NULL, false);
		data.point.x = (i*4 + 5)*32 + 16;
		data.point.y = 3*32 + 16;
		data.time = 25*20;
		anit->SetRenderData(data);
		m_aniList.push_back(anit);
	}
}

void GameRender::Update(int elapsed)
{
	g_pDraw->ShowCell();
	g_pRole->GetVisual()->Render(elapsed);
	g_pEnemy->GetVisual()->Render(elapsed);
	g_pDraw->Render();

	AnimateTest::iterator itr = m_aniList.begin();
	while (itr != m_aniList.end())
	{
		(*itr)->Draw(elapsed);
		itr++;
	}
}
